package com.breezelin.freshmarket.activities;

/**
 * Created by Breeze Lin
 * 2016/5/16 02:27
 * 1linyufeng1@gmail.com
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.adpaters.ProductsAdapter;
import com.breezelin.freshmarket.app.BaseActivity;
import com.breezelin.freshmarket.entities.data.Category;
import com.breezelin.freshmarket.entities.data.Product;
import com.breezelin.freshmarket.entities.rsp.QueryRsp;
import com.breezelin.freshmarket.network.TaskCanceler;
import com.breezelin.freshmarket.network.asynctasks.TypeQueryTask;

import java.util.ArrayList;

/**
 * 分类详情页面
 */
public class CategoryActivity extends BaseActivity implements ProductsAdapter.ProductsListener,
		View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

	/**
	 * 标题栏
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private Toolbar toolbar;
	/**
	 * 刷新控件
	 */
	private SwipeRefreshLayout refreshLayout;

	/**
	 * 列表
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private ListView listView;
	/**
	 * 列表适配器
	 */
	private ProductsAdapter adapter;
	/**
	 * 产品集合
	 */
	private ArrayList<Product> products;
	/**
	 * 当前查询目录
	 */
	private Category category;
	/**
	 * 页码
	 */
	private int pageNo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_category);

		initData();
		bindView();
		getData();
	}

	/**
	 * 进行数据查询
	 */
	private void getData() {
		new TypeQueryTask(category.getCategoryId(), pageNo) {
			@Override
			protected void onSuccess(QueryRsp queryRsp) {

				if (queryRsp.getCommodities().size() > 0) {
					// 如果页码是1，代表刷新，先清空数据
					if (pageNo == 1) {
						products.clear();
					}
					products.addAll(queryRsp.getCommodities());
					// 添加数据，刷新列表
					adapter.notifyDataSetChanged();
					if (pageNo > 1) {
						showToast(R.string.toast_page_loaded);
					}
					pageNo++;
					// 如果刷新控件在工作，停止它
					if (refreshLayout.isRefreshing()) {
						showToast(R.string.toast_page_refreshed);
						refreshLayout.setRefreshing(false);
					}
				} else {
					showToast(R.string.toast_page_end);
				}
			}
		}.execute();
	}

	/**
	 * 绑定视图
	 */
	private void bindView() {
		// 绑定视图
		toolbar = (Toolbar) findViewById(R.id.toolbar);
		listView = (ListView) findViewById(R.id.list);
		refreshLayout = (SwipeRefreshLayout) findViewById(R.id.layout_refresh);

		// 列表设置
		products = new ArrayList<>();
		adapter = new ProductsAdapter(this, products, this);
		listView.setAdapter(adapter);

		// 刷新控件
		refreshLayout.setEnabled(true);
		refreshLayout.setRefreshing(false);
		refreshLayout.setOnRefreshListener(this);

		// 设置标题栏
		setSupportActionBar(toolbar);
		// 返回键可用
		//noinspection ConstantConditions
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		toolbar.setNavigationOnClickListener(this);

		// 设置为标题
		String title = getString(R.string.category) + " " + category.getCategoryName();
		toolbar.setTitle(title);
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		// 得到需要搜索的文字序列
		category = getIntent().getParcelableExtra("category");
		pageNo = 1;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		TaskCanceler.cancelTypeRequests();
	}

	@Override
	public void onProductSelected(Product product) {
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra("id", product.getCommodityId());
		intent.putExtra("type", category);
		startActivity(intent);
	}

	@Override
	public void onPageEnd() {
		getData();
	}

	@Override
	public void onClick(View v) {
		onBackPressed();
	}

	@Override
	public void onRefresh() {
		// 重置页数，进行加载
		pageNo = 1;
		getData();
	}
}
